/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package fr.loof.fonzie.it;

import static junit.framework.Assert.assertTrue;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;

import fr.loof.fonzie.BatMobile;
import fr.loof.fonzie.Car;
import fr.loof.fonzie.JPAUtils;
import fr.loof.fonzie.User;
import fr.loof.fonzie.Vehicle;
import fr.loof.fonzie.dialects.HibernateDialect;

@ContextConfiguration( locations = { "hibernateContext.xml" } )
public class HibernateIT
    extends AbstractTransactionalJUnit4SpringContextTests
{
    @PersistenceContext
    EntityManager em;

    @Test
    public void instanceOfAndCast()
        throws Exception
    {
        JPAUtils.setJPADialect( new HibernateDialect() );

        em.createNativeQuery( "insert into Vehicle (id, type, DTYPE) values (1, 'custom', 'BatMobile')" ).executeUpdate();
        em.createNativeQuery( "insert into User (id, firstName, VEHICLE_ID) values (2, 'batMan', 1)" ).executeUpdate();
        
        User fonzie = User.findByFirstName( "batMan" );
        Vehicle vehicle = fonzie.getVehicle();
        assertTrue( vehicle.instanceOf( Vehicle.class ) );
        assertTrue( vehicle.instanceOf( Car.class ) );
        assertTrue( vehicle.instanceOf( BatMobile.class ) );

        BatMobile batMobile = vehicle.as( BatMobile.class );
    }

}
